export function getDicName( value, list ) {
  let name = list.find( ( item ) => ( item.value = value ) ).name;
  return name;
}
// 获取中心点
/**
 * @param arr [[114.9090,39.4382],[114.9090,39.4382],[114.9090,39.4382]]
 * */
export function getCenterPoint( arr ) {
  let centerPoint = []; // 存储中心点
  let jin = [];
  let wei = [];
  arr.forEach( ( item ) => {
    jin.push( item[ 0 ] );
    wei.push( item[ 1 ] );
  } );
  jin.sort();
  wei.sort();
  centerPoint[ 0 ] = ( jin[ jin.length - 1 ] + jin[ 0 ] ) / 2;
  centerPoint[ 1 ] = ( wei[ wei.length - 1 ] + wei[ 0 ] ) / 2;
  // console.log("中心点", centerPoint);
  return centerPoint;
}
// 根据 图斑的 经纬度坐标 算出 合适的放大级别
/**
 * @params  locations  二维数组
 * */
export function getGoodZoom( locations ) {
  let LngLat = getMaxMinLngLat( locations )
  // 计算最佳缩放级别
  let alt_x = LngLat.maxLng - LngLat.minLng;
  let alt_y = LngLat.maxLat - LngLat.minLat;
  let result = 0;
  if ( alt_x - alt_y > 0 ) {
    result = alt_x;
  } else {
    result = alt_y;
  }
  result = result * 10000;
  let goodZoom = getAlt( result );
  return goodZoom
}

//  获取 ALT 的值
export function getAlt( result ) {
  let ALT = 0.0;
  if ( result <= 5 ) {
    ALT = 17.0;
  } else if ( result > 5 && result <= 10 ) {
    ALT = 16.5;
  } else if ( result > 10 && result <= 15 ) {
    ALT = 16.3;
  } else if ( result > 15 && result <= 20 ) {
    ALT = 16.1;
  } else if ( result > 20 && result <= 25 ) {
    ALT = 16.0;
  } else if ( result > 25 && result <= 30 ) {
    ALT = 15.9;
  } else if ( result > 30 && result <= 35 ) {
    ALT = 15.7;
  } else if ( result > 35 && result <= 40 ) {
    ALT = 15.5;
  } else if ( result > 40 && result <= 50 ) {
    ALT = 15.0;
  } else if ( result > 50 && result <= 100 ) {
    ALT = 14.5;
  } else if ( result > 100 && result <= 150 ) {
    ALT = 14.0;
  } else if ( result > 150 && result <= 200 ) {
    ALT = 13.0;
  } else if ( result > 200 && result <= 250 ) {
    ALT = 12.5;
  } else if ( result > 250 && result <= 500 ) {
    ALT = 12.0;
  } else if ( result > 500 && result <= 600 ) {
    ALT = 11.5;
  } else if ( result > 600 && result <= 1000 ) {
    ALT = 11.0;
  } else if ( result > 1000 && result <= 2000 ) {
    ALT = 10.5;
  } else if ( result > 2000 && result <= 3000 ) {
    ALT = 10.0;
  } else if ( result > 3000 && result <= 3500 ) {
    ALT = 9.5;
  } else if ( result > 3500 && result <= 4500 ) {
    ALT = 9.3;
  } else if ( result > 4500 && result <= 5500 ) {
    ALT = 9.3;
  } else if ( result > 5500 && result <= 6000 ) {
    ALT = 9.2;
  } else if ( result > 6000 ) {
    ALT = 9.0;
  }
  return ALT;
}

// 获取 最大的经度 纬度   最小的经度 纬度
/**
 * @param  locations  二维数组
 * */
export function getMaxMinLngLat( locations ) {
  let minLng = Infinity;
  let maxLng = -Infinity;
  let minLat = Infinity;
  let maxLat = -Infinity;

  locations.forEach( ( coords ) => {
    const lng = coords[ 0 ];
    const lat = coords[ 1 ];

    if ( lng < minLng ) {
      minLng = lng;
    }

    if ( lng > maxLng ) {
      maxLng = lng;
    }

    if ( lat < minLat ) {
      minLat = lat;
    }

    if ( lat > maxLat ) {
      maxLat = lat;
    }
  } );
  let LngLat = {
    minLng: minLng,
    maxLng: maxLng,
    minLat: minLat,
    maxLat: maxLat,
  };
  return LngLat;
}